class CommodPage{
    constructor(){
        this.getGoodDetail();
        this.bindEvent();
    }
    //绑定事件
    bindEvent(){
        this.$('#addnum').onclick = this.add;
        this.$('#minnum').onclick = this.reduce;
        this.getName();
        this.$('.tuichu').onclick = this.toTuiChu;
        this.$('#toCart').onclick = this.toCart;
    } 
    //**去购物车** */
    toCart=()=>{
        //判断是否有token
        //获取数据
        let goodsId = location.href.split('=')[1];
        let id = localStorage.getItem('id');
        let AUTH_TOKEN = localStorage.getItem('token');
        let number = this.$('#num').innerText;
        //console.log(id,goodsId);
        //请求拦截器
        // 添加请求拦截器
        /* **ajax请求*** */
        //先更新购物车数据

        axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
        let data = `id=${id}&goodsId=${goodsId}`;
        let data1 = `id=${id}&goodsId=${goodsId}&number=${number}`;
        //console.log(data1);
        //添加进入购物车
        axios({
            method: 'POST',
            url:'http://localhost:8888/cart/add',
            headers: { 'content-type': 'application/x-www-form-urlencoded' },
            data,
        })
        //更新商品数据
        axios({
            method: 'POST',
            url:'http://localhost:8888/cart/number',
            headers: { 'content-type': 'application/x-www-form-urlencoded' },
            data:data1
        }).then(
            ({data}) => {
                console.log(data);
            if(data.code==1){
                location.assign('./shopingcart.html');
            }else{
                //设置登录后直接返回的url
                let returnUrl = './shopingcart.html';
                location.assign('./login.html?'+returnUrl);
            }
    })
    }
    //加
    add=()=>{
        let num = this.$('#num').innerText - 0;
        num++;
        this.$('#num').innerText = num;
    }
    //减
    reduce=()=>{
        let num = this.$('#num').innerText - 0;
        num--;
        if(num<=0){
            num = 1;
        }
        this.$('#num').innerText = num;
    }
    //获取商品详细信息
    async getGoodDetail(){
        //获取id发送请求
        let href = location.href.split('=')[1];
        //console.log(href);
        let {data,status} = await axios.get('http://localhost:8888/goods/item/'+href)
        if(status===200 && data.code){
            let {info} = data;
            //console.log(info);
            //设置html渲染页面
            this.$('.shop-name').innerText = info.title;
            this.$('.shop-price').innerText = info.current_price;
            this.$('.shop-id').innerText = info.goods_id;
            this.$('.product-gallery-img img').src = info.img_big_logo;
            this.$('.big img').src = info.img_big_logo;
            this.$('.product-gallery-nav').querySelectorAll('img').forEach(item => {
                item.src = info.img_big_logo
            });
        }
        
    }
    toTuiChu(){
        //console.log(666);
        let id = localStorage.getItem('id');
        //读取id发送请求
        axios.get('http://localhost:8888/users/logout',{
            params:{
                id,
            }
        }).then(
            res => {
                //跳转页面  清空localStorage
                localStorage.removeItem('token');
                localStorage.removeItem('name');
                localStorage.removeItem('id');
                location.assign('./login.html')
            }
        )
    }
    getName(){
        let name = localStorage.getItem('name');
        //console.log(name);
        if(!name){
            this.$('.login-after').style.display = 'none';
            this.$('.login-before').style.display = 'block';
            //console.log(this.$('.login-before').firstElementChild);
            this.$('.login-before').firstElementChild.onclick = function(){
                location.assign('./login.html?shopingcart.html')
            }
        }else{
            this.$('.welcome').innerHTML = name;
        this.$('.login-after').style.display = 'block';
        this.$('.login-before').style.display = 'none';
        }
    }
    //获取节点
    $(ele){
        let element = document.querySelectorAll(ele);
        return element.length==1? element[0] : element;
    }
}
new CommodPage;